<?php
//一些常用的操作;
class Mylib {
     #商城配置信息;   
	 function shopconfig(){
		$arr = array();
		$data = read_static_cache('shop_config');		 
		if ($data === false){
			$sql = 'SELECT code, value FROM ' . gettable('shopconfig') . ' ';
			$res = G("db")->getAll($sql);

			foreach ($res AS $row)
			{
				$arr[$row['code']] = $row['value'];
			}			
			write_static_cache('shop_config', $arr);
		}else{
			$arr = $data;
		}
		$GLOBALS['_CFG']=$arr;
		G('shopconf',(object)$arr);
		return $arr;
	 }
	  /**
     * 取得当前的域名
     *
     * @access  public
     *
     * @return  string      当前的域名
     */
    function get_domain()
    {
        /* 协议 */
        $protocol = Mylib::http();

        /* 域名或IP地址 */
        if (isset($_SERVER['HTTP_X_FORWARDED_HOST']))
        {
            $host = $_SERVER['HTTP_X_FORWARDED_HOST'];
        }
        elseif (isset($_SERVER['HTTP_HOST']))
        {
            $host = $_SERVER['HTTP_HOST'];
        }
        else
        {
            /* 端口 */
            if (isset($_SERVER['SERVER_PORT']))
            {
                $port = ':' . $_SERVER['SERVER_PORT'];

                if ((':80' == $port && 'http://' == $protocol) || (':443' == $port && 'https://' == $protocol))
                {
                    $port = '';
                }
            }
            else
            {
                $port = '';
            }

            if (isset($_SERVER['SERVER_NAME']))
            {
                $host = $_SERVER['SERVER_NAME'] . $port;
            }
            elseif (isset($_SERVER['SERVER_ADDR']))
            {
                $host = $_SERVER['SERVER_ADDR'] . $port;
            }
        }

        return $protocol . $host;
    }

    /**
     * 获得 ECSHOP 当前环境的 URL 地址
     *
     * @access  public
     *
     * @return  void
     */
    function url()
    {
		 
        $curr = strpos(__SELF__, 'index/') !== false ?
                preg_replace('/(.*)(index)(\/?)(.)*/i', '\1', dirname(__SELF__)) :
                dirname(__SELF__);

        $root = str_replace('\\', '/', $curr);

        if (substr($root, -1) != '/')
        {
            $root .= '/';
        }
         return Mylib::get_domain() . $root;
    }

    /**
     * 获得 ECSHOP 当前环境的 HTTP 协议方式
     *
     * @access  public
     *
     * @return  void
     */
    function http()
    {
        return (isset($_SERVER['HTTPS']) && (strtolower($_SERVER['HTTPS']) != 'off')) ? 'https://' : 'http://';
    }

	
  
}
?>